package com.ssx.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ssx.entity.Customer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ssx.entity.Customervisit;
import com.ssx.entity.Employees;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ssx
 * @since 2020-10-24
 */
public interface CustomerMapper extends BaseMapper<Customer> {

    @Select("select distinct customer.*,employees.* from customer left join employees on employees.empid=customer.empid where employees.empid=#{id}")
    @Results({
            @Result(
                    javaType = Employees.class,
                    column = "empid",
                    property = "employees",
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            )
    })
    List<Customer> selectCusEmpid(int id);

    @Select("select distinct customer.* from customer left join employees on employees.empid=customer.empid where cusId=#{id}")
    @Results({
            @Result(
                    javaType = Employees.class,
                    column = "empid",
                    property = "employees",
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            )
    })
    Customer selectById(int id);

    @Select("SELECT distinct customer.*,employees.`name` FROM customershare\n" +
            "LEFT JOIN customer ON customershare.cusid=customer.cusId\n" +
            "LEFT JOIN employees ON employees.empid=customershare.empid ${ew.customSqlSegment}")
    @Results({
            @Result(column = "empid",
                    property = "employees",
                    javaType = Employees.class,
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            ),
//            @Result(column = "employees.empid",property = "employees.empid"),
//            @Result(column = "employees.name",property = "employees.name")
    })
        //员工查询客户，记录
    List<Customer> listFromEmp(@Param(Constants.WRAPPER) Wrapper<Customer> wrapper);

    @Select("SELECT distinct customer.*,employees.`name` FROM customershare\n" +
            "LEFT JOIN customer ON customershare.cusid=customer.cusId\n" +
            "LEFT JOIN employees ON employees.empid=customer.empid ${ew.customSqlSegment}")
    @Results({
            @Result(column = "empid",
                    property = "employees",
                    javaType = Employees.class,
                    one = @One(select = "com.ssx.mapper.EmployeesMapper.selectById")
            ),
//            @Result(column = "employees.empid",property = "employees.empid"),
//            @Result(column = "employees.name",property = "employees.name")
    })
        //经理查询客户，记录
    List<Customer> listFromManger(@Param(Constants.WRAPPER) Wrapper<Customer> wrapper);

    @Select("SELECT distinct count(*) FROM  customer\n" +
            "LEFT JOIN customershare ON customershare.cusid=customer.cusId\n" +
            "LEFT JOIN employees ON employees.empid=customershare.empid ${ew.customSqlSegment}")
    //员工查询客户，总数
    public int countFromEmp(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT distinct count(*) FROM customer\n" +
            "LEFT JOIN customershare ON customershare.cusid=customer.cusId\n" +
            "LEFT JOIN employees ON employees.empid=customer.empid ${ew.customSqlSegment}")
    //经理查询客户，总数
    public int countFromManger(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Insert("insert into customer(cusName,address,contact,tel,email,empid)values(#{cusName},#{address},#{contact},#{tel},#{email},#{employees.empid}) ")
    @Override
    int insert(Customer entity);

    @Update("update customer set cusName=#{cusName},address=#{address},contact=#{contact},tel=#{tel},email=#{email},empid=#{employees.empid} where cusId=#{cusId}")
    @Override
    int updateById(Customer entity);
}
